<?php

class Con_login extends Acan{
	public $defaultAct='go';
	public $Conread = '登入管理';
	public $Actread = array('go'=>'登入验证','out'=>'退出登入');
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
	}
	//登入验证
	function Act_aweb(){
		if($_POST){
			if($_POST['hash'] != Afun_auth_hash($_POST['uid'],$_POST['time'])){
				echo '错误'; return;
			}
			$_SESSION['web'] = $_POST;
			Afun_refresh('',"http://aban.nice9s.com/web",100);
		}
	}
	function Act_awebset(){
		setcookie($_GET['key'],$_GET['val'],time()+7200,"/",'aban.nice9s.com');
		Afun_refresh('',"http://aban.nice9s.com/web",100);
	}
	//登入验证
	function Act_go(){
		$login = $_POST;
		if($login['Autho']){$Autho = Afun_arr_obj($login['Autho']);}//认证登入
		if(empty($login['name']) && empty($login['mail'])){
			$msg = "帐号不能为空";
			$status = 2;
		}elseif(empty($login['pass']) && !is_object($Autho)){
			$msg = "密码不能为空";
			$status = 3;
		}else{
			$con->select = "a.uid,a.username,a.mail,a.password";
			$con->where = "code='$login[name]' or username='$login[name]' or mail='$login[name]'";
			if($login['mail']){$con->where = "mail='$login[mail]'";}
			if($Autho->uid>0){$con->where = 'a.uid='.$Autho->uid;}
			$model = Afun_db()->model(Acan_table_user);
			$data = $model->ofind($con);
			if(empty($data)){
				$msg = "帐号不存在!";
				$status = 2;
			}else{
				$Auth_status = false;
				if($data->password == Afun_pass($login['pass'])){
					$Auth_status = true;
				}
				if($Autho->hash == Afun_auth_hash($Autho->uid,$Autho->time)){
					$Auth_status = true;
				}
				if($Auth_status){
					$status = 1;
					$msg = "登入成功";
					$ss['uid'] = $data->uid;
					$ss['user'] = $data->username;
					$ss['mail'] = $data->mail;
					if($_POST['Auth']){
						$fdate = Afun_db()->model(Acan_table_fate)->find('uid='.$data->uid);
						$Auth['fid'] = $fdate['fid'];
						$Auth['oid'] = $fdate['oid'];
					}
					if($_POST['Auth']['ip']){
						$Auth['ip'] = $_POST['Auth']['ip'];
					}else{
						$Auth['ip'] = Afun_get_ip();
					}
					$Auth['uid'] = $data->uid;
					$Auth['mail'] = $data->mail;
					$Auth['time'] = (string)Afun_get_time("time");
					$Auth['hash'] = Afun_auth_hash($Auth['uid'],$Auth['time']);
					$ss['Auth'] = $Auth;
					$ss['group'] = Afunc_group()->getgroup($data->uid);
					$ss['group_enum'] = Afunc_group()->enum();
					$ss['group_title'] = $ss['group_enum'][$ss['group']];
					$ss['admin'] = Afunc_group()->getgroupadmin($data->uid);
					Afun_session($ss);unset($ss['group_enum']);
					Afunc_user()->load_info($data->uid);
					$model->updatePk(array('lastdate'=>Afun_get_time(),'lastip'=>$Auth['ip']),$data->uid);
				}else{
					$status = 3;
					$msg = "密码错误";
				}
			}
		}
		/*
		if($_POST['name'] == Acan_auth_name && $_POST['pass'] == Acan_auth_pass){
			$_SESSION['uid'] = '1';
			$_SESSION['user'] = Acan_auth_name;
			$_SESSION['app'][] = Acan_app_name;
			$msg = "登入成功";
			$status = 1;
			if(Acan_auth_pass==''){
				$msg =  '<br>你的密码为空，请尽快到 设置 - Anmp设置 设置Anmp控制台登入信息！';
			}
		}else{
			$msg =  '密码或者用户名错误';
		}*/
		$rs->Auth = $Auth;
		$rs->data = $ss;
		$rs->msg = $msg;
		$rs->status = $status;
		Afunc_log('log')->write(json_encode(array($_POST,$rs)),'danhao');
		Afun_json($rs);
	}
	//退出
	function Act_out(){
		//unset($_SESSION['uid']);
		session_destroy();
		$rs->msg = "退出成功";
		$rs->status = 1;
		Afun_json($rs);
	}
}

?>